﻿using System;
using System.Linq;
using System.Collections.Generic;
using HQPad.DAL;
using HQPad.Models;


namespace HQPad.Services
{
    public interface IPostingService
    {
        IEnumerable<Posting> GetPostings(string group);
        Posting GetPostingByKey(string group, string key);
        Posting GetPostingById(int id);
        void SavePosting(Posting posting, string userName);
        void DeletePosting(int id);
        Posting CreatePosting(string groupName, string key, string userName);
        void Add(Posting posting);
    }

    public class PostingService : IPostingService
    {
        private MembershipContext context = null;

        public PostingService(MembershipContext context)
        {
            this.context = context;
        }

        public Posting CreatePosting(string groupName, string key, string userName)
        {
            Posting posting = new Posting() { 
                PostingGroup = groupName,
                PostingKey = key,
                Created = DateTime.Now,
                LastModified = DateTime.Now,
                UserName = userName,
                Content = string.Empty
            };

            this.context.Postings.Add(posting);

            return posting;
        }

        public void Add(Posting posting)
        {
            this.context.Postings.Add(posting);
        }

        public Posting GetPostingByKey(string groupName, string key)
        {

            return (from p in context.Postings
                    where p.PostingKey == key && p.PostingGroup == groupName
                    select p)
                    .FirstOrDefault();
        }

        public Posting GetPostingById(int id)
        {
            return (from p in context.Postings
                    where p.PostingID == id
                    select p)
                   .FirstOrDefault();
        }

        public IEnumerable<Posting> GetPostings(string groupName)
        {
            return (from p in context.Postings
                    where p.PostingGroup == groupName
                    select p);
        }
        
        public void SavePosting(Posting posting, string userName)
        {
            posting.LastModified = DateTime.Now;
            posting.UserName = userName;

            this.context.Entry<Posting>(posting).State = System.Data.EntityState.Modified;
        }

        public void DeletePosting(int id)
        {
            Posting posting = GetPostingById(id);

            if (posting != null)
                this.context.Entry<Posting>(posting).State = System.Data.EntityState.Deleted;

        }

        public static string MakeSlug(Posting posting)
        {
            return MakeSlug(posting.PostingGroup, posting.PostingKey);
        }

        public static string MakeSlug(string group, string key)
        {
            return
                string.Format("{0}_{1}",
                    group
                    .Replace("_", "")
                    .Replace("-", "")
                    .Replace("&", "")
                    .Replace("+", "")
                    .Replace(",", "")
                    .Replace(".", "")
                    .Replace(" ", "-")
                    .Replace("--", "-")
                    .Replace("--", "-")
                    .Replace("'", ""),
                    key
                    .Replace("_", "")
                    .Replace("-", "")
                    .Replace("&", "")
                    .Replace("+", "")
                    .Replace(",", "")
                    .Replace(".", "")
                    .Replace(" ", "-")
                    .Replace("--", "-")
                    .Replace("--", "-")
                    .Replace("'", ""));

        }
    }
}